from django.contrib.syndication.feeds import Feed
from blog.models import Post, Artist
from django.core.paginator import Paginator, InvalidPage
from django.views.generic.list_detail import object_list
from django.shortcuts import render_to_response
from django.template import RequestContext
from gig.models import Gig
from music.models import *
from link.models import *
from django.contrib.comments.models import Comment
from name_paginator import NamePaginator, NamePage

from datetime import datetime

class LatestPost(Feed):
	title = "sam - rss news"
	link = "/blog/"
	description = "Le flux rss des news du site de sam"
	def items(self):
		return Post.objects.order_by('-date')[:5]
	item_author_link = 'http://www.sam-lesite.fr/'
	item_author_name = 'sam' 
	def item_pubdate(self, item):
	    return datetime(item.date.year, item.date.month, item.date.day)
	
def post_list(request):
    q = Post.objects.all().order_by('-date')
    t = datetime.now()
    t.strftime("%Y-%m-%d %H:%M:%S")
    next_gig = Gig.objects.filter(date_time__gt = t).order_by('date_time')[0:1][0]
    last_album = Album.objects.order_by('-date')[0:1][0]
    page = int(request.GET.get('page', '1')) 
    recent_comments = Comment.objects.order_by('-submit_date')[0:3]
    context = "blog"
    return object_list(request, queryset=q, paginate_by=5, extra_context={'last_album':last_album, 'next_gig': next_gig,'page':page, 'recent_comments':recent_comments, 'context':context}) 
    #return object_list(request, queryset=q, paginate_by=5)

def category_detail(request, category_slug):
    q = Post.objects.all().order_by('-date')
    t = datetime.now()
    t.strftime("%Y-%m-%d %H:%M:%S")
    next_gig = Gig.objects.filter(date_time__gt = t).order_by('date_time')[0:1]
    page = int(request.GET.get('page', '1')) 
    recent_comments = Comment.objects.order_by('-submit_date')[0:3]
    context = "blog"
    return object_list(request, queryset=q, paginate_by=5, extra_context={'next_gig': next_gig,'page':page, 'recent_comments':recent_comments, 'context':context}) 

def artists(request):
    artists = Artist.objects.all().order_by('name')
    paginator = NamePaginator(artists, on="name", per_page=25)
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    try:
        page = paginator.page(page)
    except (InvalidPage):
        page = paginator.page(paginator.num_pages)
    context = "artist"
    return render_to_response('blog/artists.html', {"page": page, "context":context}, context_instance=RequestContext(request))	
	#return render_to_response('blog/artists.html', {'artists':artists}, context_instance=RequestContext(request))

def update_position(request, object_id, x_position, y_position):
	artist = Artist.objects.get(id = object_id)
	artist.x_position = x_position
	artist.y_position = y_position
	artist.save()
	return True

def artist_posts(request, object_id):
    artists = Artist.objects.all().order_by('name')
    artist = Artist.objects.get(id=object_id)
    context = "artist"
    return render_to_response('blog/artist.html',  {'artist':artist, 'artists':artists, "context":context}, context_instance=RequestContext(request))



from contact_form.forms import ContactForm
from django.core.paginator import Paginator


def dashboard(request):
    t = datetime.now()
    t.strftime("%Y-%m-%d %H:%M:%S")
    next_gig = Gig.objects.filter(date_time__gt = t).order_by('date_time')[0:1]
    artists_random = Artist.objects.order_by('?')[0:3]
    albums = Album.objects.all().filter(album_category=1).filter(publish=1).order_by('-date')
    recent_comments = Comment.objects.order_by('-submit_date')[0:2]
    post_list = Post.objects.all().order_by('-date')
    paginator = Paginator(post_list, 5)
    try:
        page = int(request.GET.get('page', '1'))
    except ValueError:
        page = 1
    # If page request (9999) is out of range, deliver last page of results.
    try:
        posts = paginator.page(page)
    except (EmptyPage, InvalidPage):
        posts = paginator.page(paginator.num_pages)
    links_ramdom = Link.objects.order_by('?')[0:3]
    contact_form = ContactForm(request=request)
    return render_to_response('site/dashboard.html',  {'contact_form':contact_form,'links_ramdom':links_ramdom, 'posts':posts, 'next_gig':next_gig, 'artists_random':artists_random, 'albums':albums, 'recent_comments':recent_comments}, context_instance=RequestContext(request))




    
    
    
    
    